package com.bootdo.contract.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.contract.domain.TestpaperDO;
import com.bootdo.contract.domain.TestpapertimuDO;
import com.bootdo.contract.domain.TikuDO;
import com.bootdo.contract.domain.TimuDO;
import com.bootdo.contract.service.TestpaperService;
import com.bootdo.contract.service.TestpapertimuService;
import com.bootdo.contract.service.TikuService;
import com.bootdo.contract.service.TimuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yang
 * @email 2795210596@qq.com
 * @date 2019-09-09 17:21:35
 */

@Controller
@RequestMapping("/contract/testpapertimu")
public class TestpapertimuController {
    @Autowired
    private TestpapertimuService testpapertimuService;
    @Autowired
    private TestpaperService testpaperService;
    @Autowired
    private TikuService tikuService;
    @Autowired
    private TimuService timuService;

    @GetMapping()
    String Testpapertimu(@RequestParam("id") Integer id, Model model) {
        model.addAttribute("id", id);
        return "contract/testpapertimu/testpapertimu";
    }

    @ResponseBody
    @GetMapping("/list")
    public PageUtils list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<TestpapertimuDO> testpapertimuList = testpapertimuService.list(query);
        int total = testpapertimuService.count(query);
        PageUtils pageUtils = new PageUtils(testpapertimuList, total);
        return pageUtils;
    }

    @ResponseBody
    @GetMapping("/kutimu")
    public String kutimu(@RequestParam("kuid") Integer kuid) {
        Map<String, Object> map = new HashMap<>();
        map.put("kuid", kuid);
        List<TimuDO> listtimu = timuService.listtimu(map);
        JSONArray array = JSONArray.parseArray(JSON.toJSONString(listtimu));
        return array.toJSONString();
    }

    @GetMapping("/add")
    String add(@RequestParam("paperid") Integer paperid, Model model, HttpServletRequest request) {
        TestpaperDO testpaperDO = testpaperService.get(paperid);
        HttpSession session = request.getSession();
        String yuid = (String) session.getAttribute("yuid");
        Map<String, Object> map = new HashMap<>();
        map.put("yuid", yuid);
        List<TikuDO> list = tikuService.list(map);
        model.addAttribute("list", list);
        model.addAttribute("paperid", paperid);
        return "contract/testpapertimu/add";
    }

    @GetMapping("/open")
    String openpuanduan(@RequestParam("id") Integer id, Model model, @RequestParam("type") Integer type) {
        Map<String, Object> map = new HashMap<>();
        map.put("titimu", type);
        map.put("kuid", id);
        List<TimuDO> listtimu = timuService.listtimu(map);
        model.addAttribute("listtimu", listtimu);
        model.addAttribute("type", type);
        return "contract/testpapertimu/open";
    }

    @GetMapping("/edit/{id}")
    String edit(@PathVariable("id") Integer id, Model model) {
        TestpapertimuDO testpapertimu = testpapertimuService.get(id);
        model.addAttribute("testpapertimu", testpapertimu);
        return "contract/testpapertimu/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    public R save(TestpapertimuDO testpapertimu) {
        testpapertimu.setCreatetime(new Date());
        if (testpapertimuService.save(testpapertimu) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    public R update(TestpapertimuDO testpapertimu) {
        testpapertimuService.update(testpapertimu);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    public R remove(Integer id) {
        if (testpapertimuService.remove(id) > 0) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    public R remove(@RequestParam("ids[]") Integer[] ids) {
        testpapertimuService.batchRemove(ids);
        return R.ok();
    }

}
